<?php
use Symfony\Component\HttpKernel\Controller\ControllerReference;

echo $view['actions']->render(new ControllerReference('FiestissimaCRM:Index:header'));

$totalDiarioContado = 0;
$totalDiarioTarjeta = 0;
$totalDiarioTotal = 0;

if(count($ordenes) > 0)
{
    $stringPanel = "<div class=\"panel-group\" id=\"accordion\" role=\"tablist\" aria-multiselectable=\"true\" id=\"panelOrdenesCompra\">";
    $rowId = 1;
    foreach($ordenes as $oc)
    {
        $totalDiarioContado += $oc->getOrdenCompraContado();
        $totalDiarioTarjeta += $oc->getOrdenCompraTarjeta();
        $totalDiarioTotal += $oc->getOrdenCompraTotal();

        $stringPanel .= "<div class=\"panel-group success\" id=\"accordionOrdenesCompra\" role=\"tablist\" aria-multiselectable=\"true\">
                          <div class=\"panel panel-default\">
                            <div class=\"panel-heading\" role=\"tab\" id=\"heading{$rowId}\">
                              <table class=\"table table-hover\" style=\"margin-bottom: 0px;\">
                                <tbody>
                                    <tr><td>Orden: {$oc->getOrdenCompraId()}</td><td>Subtotal Contado: $ {$oc->getOrdenCompraContado()}</td><td>Subtotal Tarjeta: $ {$oc->getOrdenCompraTarjeta()}</td><td>Total: $ {$oc->getOrdenCompraTotal()}</td><td><a data-toggle=\"collapse\" data-parent=\"#accordionOrdenesCompra\" href=\"#collapse{$rowId}\" aria-expanded=\"true\" aria-controls=\"collapse{$rowId}\"><button id=\"detalle{$rowId}\" type=\"button\" class=\"btn btn-primary btn-sm\" data-toggle=\"collapse\" data-parent=\"#accordionOrdenesCompra\" aria-expanded=\"true\" aria-controls=\"collapse{$rowId}\"><i class=\"fa fa-edit fa-fw\"></i> Ver detalles</button>
                                </a></td></tr>
                                </tbody>
                              </table>
                            </div>
                            <div id=\"collapse{$rowId}\" class=\"panel-collapse collapse\" role=\"tabpanel\" aria-labelledby=\"heading{$rowId}\">
                              <div class=\"panel-body\">";
        if($oc->getCliente() !== null){$stringPanel .= "Cliente: {$oc->getCliente()}";}
        $stringPanel .= "<table class=\"table table-hover\">
                            <thead>
                                <tr><th colspan=\"3\">Detalle Producto</th></tr>
                            </thead>
                            <tbody>";
        foreach($oc->getOrdenCompraProducto() as $opr) {
            $stringPanel .= "<tr><td>{$opr->getProducto()}</td><td>{$opr->getOrdenCompraProductoCantidad()}</td><td>Precio TBD</td></tr>";
        }
        $stringPanel .= "</tbody>
                            </table>
                            <table class=\"table table-hover\">
                                <thead>
                                    <tr><th colspan=\"2\">Formas de pago</th></tr>
                                </thead>
                                <tbody>";
        foreach($oc->getOrdenCompraPago() as $opg) {
            $stringPanel .= "<tr><td>{$opg->getFormaPago()}</td><td>{$opg->getOrdenCompraPagoSubtotal()}</td></tr>";
        }
        $stringPanel .= "</tbody>
                    </table>

                    </div>
                </div>
              </div>
            </div>";
        $rowId++;
    }
}else{
    $stringPanel = "No hay órdenes de compra del día de hoy.";
}
?>
        <div id="page-wrapper">
            <div class="row">
                <div class="col-lg-12">
                    <h2 class="page-header">Órdenes de compra </h2>
                </div>
                <!-- /.col-lg-12 -->
            </div>
            <!-- /.row -->
            <div class="row">
                <div class="col-lg-12">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <i class="fa fa-table fa-fw"></i>                                
                            <button id="agregarOrdenCompraButton" type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#agregarOrdenCompraModal">
                            	Agregar Orden de Compra
                            </button>
                            Contado: <?php echo $totalDiarioContado; ?> Tarjeta: <?php echo $totalDiarioTarjeta; ?> Total: <?php echo $totalDiarioTotal; ?>
                        </div>
                        <!-- /.panel-heading -->
                        <div class="panel-body">
                            <div class="row">
                                <div class="col-lg-12">
                                    <div class="table-responsive" id="divTableDiarioProductos">
                                        <?php echo $stringPanel; ?>
                                    </div>
                                    <!-- /.table-responsive -->
                                </div>
                                <!-- /.col-lg-4 (nested) -->
                            </div>
                            <!-- /.row -->
                        </div>
                        <!-- /.panel-body -->
                    </div>
                    <!-- /.panel -->
                </div>
                <!-- /.col-lg-12 -->
            </div>
            <!-- /.row -->
        </div>
        <!-- /#page-wrapper -->

    <!-- agregarOrdenCompraModal -->
    <div class="modal fade" id="agregarOrdenCompraModal" tabindex="-1" role="dialog" aria-labelledby="agregarOrdenCompraModalLabel" aria-hidden="true">
        <div class="modal-dialog" style="width:95%;">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="agregarOrdenCompraModalLabel">Nueva Orden de Compra</h4>
                </div>
                <div class="modal-body">
                    <!-- **** FORMULARIO SELECCIONAR CLIENTE **** -->
                    <form id="formOrdenCompraCliente">
                    <div class="form-group row">
                        <div class="col-sm-4">
                            <label for="selectCliente" class="control-label">Cliente</label>
                            <select id="selectCliente" class="form-control">
                                <?php
                                foreach($clientes as $c)
                                {
                                    echo "<option value=\"{$c->getCId()}\">{$c}</option>";
                                }
                                ?>
                            </select>
                        </div>
                        <div class="col-sm-4">
                            <label for="quitarAgregarCliente" class="control-label">&nbsp;</label>
                            <div><button id="quitarAgregarCliente" class="btn btn-warning" type="button">Quitar Cliente</button></div>
                        </div>
                    </div>
                    </form>

                    <!-- **** FORMULARIO ORDENCOMPRA-PRODUCTO **** -->
                    <form id="formOrdenCompraProducto">
                    <div class="form-group row">
                        <div class="col-sm-6">
                            <label for="inputInstancia" class="control-label">Producto</label>
                            <?php echo $view['form']->row($formOrdenCompraProducto['producto'], array('attr' => array('class' => 'form-control'), 'label' => false));?>
                        </div>
                        <div class="col-sm-2">
                            <label for="inputInstancia" class="control-label">Cantidad</label>
                            <?php echo $view['form']->row($formOrdenCompraProducto['orden_compra_producto_cantidad'], array('attr' => array('value' => '1', 'class' => 'form-control'), 'label' => false));?>
                        </div>
                        <div class="col-sm-2">
                            <label for="inputInstancia" class="control-label">Subtotal</label>
                            <input type="text" class="form-control" required="required" name="productoSubtotal" id="productoSubtotal" readonly="readonly">
                        </div>
                        <div class="col-sm-2">
                            <label for="inputInstancia" class="control-label">&nbsp;</label>
                            <div><button id="agregarProductoADetalle" class="btn btn-primary" type="button">Agregar Producto</button></div>
                        </div>
                    </div>
                    </form>

                    <!-- **** FORMULARIO ORDENCOMPRA-PAGO **** -->
                    <form id="formOrdenCompraPago">
                    <div class="form-group row">
                        <div class="col-sm-4">
                            <label for="inputInstancia" class="control-label">Forma de Pago</label>
                            <?php echo $view['form']->row($formOrdenCompraPago['formaPago'], array('attr' => array('class' => 'form-control'), 'label' => false));?>
                        </div>
                        <div class="col-sm-4">
                            <label for="inputInstancia" class="control-label">Importe</label>
                            <?php echo $view['form']->row($formOrdenCompraPago['orden_compra_pago_subtotal'], array('attr' => array('class' => 'form-control'), 'label' => false));?>
                        </div>
                        <div class="col-sm-4">
                            <label for="inputInstancia" class="control-label">&nbsp;</label>
                            <div><button id="agregarFormaPago" class="btn btn-primary" type="button">Agregar Forma de Pago</button></div>
                        </div>
                    </div>
                    </form>

                    <!-- **** FORMULARIO ORDEN DE COMPRA **** -->
                    <form id="formOrdenCompra">
                    <div class="form-group row">

                        <div class="col-sm-4">
                            <label for="inputInstancia" class="control-label">Contado</label>
                            <?php echo $view['form']->row($formOrdenCompra['orden_compra_contado'], array('attr' => array('class' => 'form-control'), 'label' => false, 'read_only' => true));?>
                        </div>

                        <div class="col-sm-4">
                            <label for="inputInstancia" class="control-label">Tarjeta</label>
                            <?php echo $view['form']->row($formOrdenCompra['orden_compra_tarjeta'], array('attr' => array('class' => 'form-control'), 'label' => false, 'read_only' => true));?>
                        </div>

                        <div class="col-sm-4">
                            <label for="inputInstancia" class="control-label">Total</label>
                            <?php echo $view['form']->row($formOrdenCompra['orden_compra_total'], array('attr' => array('class' => 'form-control'), 'label' => false, 'read_only' => true));?>
                        </div>
                    </div>
                    </form>

                    <div class="form-group row">
                        <div class="col-sm-12">
                            <button id="guardarOrdenCompra" class="btn btn-success" type="button">Guardar</button>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <table id="ventaDetalle" class="table table-bordered table-hover table-striped">
                        <thead>
                            <tr>
                                <th>Producto</th>
                                <th>Unidades</th>
                                <th id="subtotal">Subtotal</th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>

                    <table id="formaPagoDetalle" class="table table-bordered table-hover table-striped">
                        <thead>
                        <tr>
                            <th>Forma de Pago</th>
                            <th>Importe</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
	    <!-- /.modal-dialog -->
		<!-- /.agregarProductoModal -->
    <!-- /#wrapper -->
    <div class="modal fade" id="confirmarOCPendientePago" tabindex="-1" role="dialog" aria-labelledby="confirmarOCPendientePagoLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" id="confirmarOCPendientePagoLabel">Orden de Compra</h4>
                </div>
                <div class="modal-body">
                    <p>La orden de compra se guardará como pendiente de pago. ¿Está de acuerdo?</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" id="btnConfirmarOCPendientePago">Si</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">No</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
<?php echo $view->render('FiestissimaCRM:Default:footer.html.php'); ?>
